<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/headeradmin.php");
?>

<section>
<hgroup>
<h1>Kamer aanpassen</h1>
</hgroup>

<?php if(!isset($_POST['Submit'])) { 
    $query="SELECT * FROM roomtype WHERE Id='" .$_GET["id"]."'";
    $result = mysql_query($query) or die ("FOUT1: " . mysql_error());

    // gegevens ophalen en toekennen aan tijdelijke variabelen
    while ($rij = mysql_fetch_array($result)){
            $Name = $rij['Name']; 
            $Activated = $rij['Activated'];
    }
?>
<form  name="kamerform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
	<tr>
            <td>Naam:</td>
            <td><input required="required" placeholder="Naam" value="<?php echo $Name; ?>" name="naam" type="text"></td>
	</tr>
        <tr>
            <td>Geactiveerd:</td>
            <td>
                <div class="onoffswitch">
                    <input type="checkbox" name="activatie" class="onoffswitch-checkbox" id="myonoffswitch" <?php if ($Activated == "T") { echo "checked"; } ?>>
                    <label class="onoffswitch-label" for="myonoffswitch">
                        <div class="onoffswitch-inner">
                            <div class="onoffswitch-active"><div class="onoffswitch-switch">JA</div></div>
                            <div class="onoffswitch-inactive"><div class="onoffswitch-switch">NEE</div></div>
                        </div>
                    </label>
                </div>
            </td>
        </tr>
 	<tr>
            <td><input type="hidden" name="id" value="<?php echo($_GET["id"]);?>" /><input type="submit" name="Submit" value="Opslaan"></td>
            <td><input type="button" value="Terug" onclick="javascript:history.back();" /></td>
        </tr>
</table>
</form>
<?php 
} 
else 
{	
	$error = array();
	
        if($_POST['naam'] == "") {
		$error[] = 'Er is geen kamer-naam ingevuld.';
	}
        
        if(isset($_POST['activatie'])) {
            $activatie = "T";
	}
        else
        {
            $activatie = "F";
        }
        
        $errors = sizeof($error); // aantal errors tellen
        if($errors != 0) { // Er is minstens 1 error
            echo "<span style=\"color:red\">Er kon geen kamer worden aangemaakt omwille van de volgende reden(en):</span>";
            echo "<ul>";
                for($i = 0; $i < $errors; $i++) {
                        echo "<span style=\"color:red\"><li>".$error[$i]."</li></span>";
                }
            echo "</ul>";
        } 
        else 
        {
            $query = "UPDATE roomtype SET Name='$_POST[naam]', Activated='$activatie' WHERE Id = '$_POST[id]'";
            mysql_query($query) or die ("FOUT2: " . mysql_error());
            echo "De kamer is aangepast.<br>";
            echo "Klik <a href=\"kamers.php\">hier</a> om terug te gaan naar het overzicht.";
        }
} 
?>
</section>

<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>
